package idp

import (
	"fmt"
	"gin-sso/conf"
	"gin-sso/dal/query"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var DB *gorm.DB

func initDB() *gorm.DB {
	DB, err := gorm.Open(mysql.Open(
		fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
			conf.GlobalConf.Mysql.User,
			conf.GlobalConf.Mysql.Password,
			conf.GlobalConf.Mysql.Host,
			conf.GlobalConf.Mysql.Port,
			conf.GlobalConf.Mysql.DBName)), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		fmt.Println(`failed to connect database:`, err)
		panic(err)
	}

	query.SetDefault(DB)
	return DB
}
